<?php

/**
 * empleado actions.
 *
 * @package    zing
 * @subpackage empleado
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class empleadoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
//    $this->empleados = Doctrine_Core::getTable('Empleado')
//      ->createQuery('a')
//      ->execute();

    $this->getContext()->getInstance()->getConfiguration()->loadHelpers("Url"); 


        $table = new sfTable(array('name'=>'tEmpleados','model'=>'empleado'),array('class'=>'pedidos')); //border:1px solid;',
        $table->thSchema->setDefaultSort('nombre');
        $table->thSchema->setDefaultSortDirection('asc');         
        $table->tdSchema->setOption('defaultTdAttributes',array('style'=>"text-align: center;width: 84px; overflow:hidden;margin-left:3px"));         

        //------
        $table->thSchema['id'] = new sfTh(array('text'=>'Cod. Empleado'));
        $table->thSchema['id']->setFilterType('number')
                             ->setFilterable(true,'Codigo Empleado');
        $table->tdSchema['id'] = new sfTdModel(array('field'=>'id'));         

        //------        
        $table->thSchema['nombre'] = new sfTh(array('text'=>'Nombre'));    
        $table->thSchema['nombre']->setFilterable(true,'Nombre')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        $table->tdSchema['nombre'] = new sfTdModel(array('field'=>'Persona.nombre'));
        
        $table->thSchema['apellido'] = new sfTh(array('text'=>'Apellido'));    
        $table->thSchema['apellido']->setFilterable(true,'Apellido')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        $table->tdSchema['apellido'] = new sfTdModel(array('field'=>'Persona.apellido'));
        

        $table->thSchema['numeroLegajo'] = new sfTh(array('text'=>'Legajo'));    
        $table->thSchema['numeroLegajo']->setFilterable(true,'Legajo')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        $table->thSchema['numeroLegajo']->setSortable(true,'Legajo');        
        $table->tdSchema['numeroLegajo'] = new sfTdModel(array('field'=>'numeroLegajo'));

        
        $table->thSchema['activo'] = new sfTh(array('text'=>'Activo'));    
        $table->thSchema['activo']->setFilterable(false);
        $table->thSchema['activo']->setSortable(false);        
        $table->tdSchema['activo'] = new sfTdModel(array('field'=>'activo'));

        
        $table->thSchema['borrado'] = new sfTh(array('text'=>'Borrado'));    
        $table->thSchema['borrado']->setFilterable(false);
        $table->thSchema['borrado']->setSortable(false);        
        $table->tdSchema['borrado'] = new sfTdModel(array('field'=>'borrado'));
        
//------               
        
        $table->thSchema['herramientas'] = new sfTh(array('text'=>'Herramientas','sortable'=>false),array('style' => 'text-align: center; width:50px;'));                
        $table->setPageSize(15);
        $table->setAction('empleado/index');
        $table->buildQuery($request);        
        $table->execute($request);        
        
        $this->tabla =  $table;
    
      
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new EmpleadoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new EmpleadoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($empleado = Doctrine_Core::getTable('Empleado')->find(array($request->getParameter('id'))), sprintf('Object empleado does not exist (%s).', $request->getParameter('id')));
    $this->form = new EmpleadoForm($empleado);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($empleado = Doctrine_Core::getTable('Empleado')->find(array($request->getParameter('id'))), sprintf('Object empleado does not exist (%s).', $request->getParameter('id')));
    $this->form = new EmpleadoForm($empleado);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($empleado = Doctrine_Core::getTable('Empleado')->find(array($request->getParameter('id'))), sprintf('Object empleado does not exist (%s).', $request->getParameter('id')));
    $empleado->delete();

    $this->redirect('empleado/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $empleado = $form->save();

      $this->redirect('empleado/edit?id='.$empleado->getId());
    }
  }
}
